package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.LoadCarDetailActivity;
import com.tianxin.workbao.workBao.Activity.LoadCarOptionActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.LineUp;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.AlertUtils;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/6/22.
 */

public class LineUpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int columns;
    private List<LineUp> mLineUps;
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private Context mContent;

    public LineUpAdapter(List<LineUp> lineUp, Context context) {
        this.mLineUps = lineUp;
        this.mContent = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.load_car_item, parent, false);
            return new LoadCarListViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof LoadCarListViewHolder) {
            LoadCarListViewHolder viewHolder = (LoadCarListViewHolder) holder;
            viewHolder.bindViewData(mLineUps.get(position));
            viewHolder.setViewListener(mLineUps.get(position));
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (mLineUps.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public int getItemCount() {
        if (mLineUps.isEmpty()) {
            return 1;
        } else {
            return mLineUps.size();
        }
    }


    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return columns;
        }
    }

    class LoadCarListViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mItemView;
        TextView carNumber;
        TextView coalName;
        TextView driver;
        TextView driverPhone;
        TextView customer;
        TextView loadStatus;
        TextView addTimeTv;


        public LoadCarListViewHolder(View itemView) {
            super(itemView);
            mItemView = (LinearLayout) itemView.findViewById(R.id.item_view);
            carNumber = (TextView) itemView.findViewById(R.id.car_number);
            coalName = (TextView) itemView.findViewById(R.id.coal_name);
            driver = (TextView) itemView.findViewById(R.id.driver_name);
            driverPhone = (TextView) itemView.findViewById(R.id.driver_phone);
            customer = (TextView) itemView.findViewById(R.id.customer);
            loadStatus = (TextView) itemView.findViewById(R.id.status_label);
            addTimeTv = (TextView) itemView.findViewById(R.id.add_time);
        }

        public void bindViewData(LineUp lineUp) {
            carNumber.setText(lineUp.getCarnumber());
            coalName.setText(lineUp.getCoaltype());
            driver.setText(lineUp.getCarowner());
            driverPhone.setText(lineUp.getCarconnectphone());
            customer.setText(lineUp.getBuycompany());
            addTimeTv.setText(TimeUtils.parseDate(lineUp.getAddtime()));
            if (lineUp.getIsloadcar() == 2) {
                if (lineUp.getLoaduserid() == SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0)) {
                    loadStatus.setText(mContent.getResources().getString(R.string.me_loaded));
                } else {
                    loadStatus.setText(mContent.getResources().getString(R.string.loaded));
                }
            } else if (lineUp.getIsloadcar() == 0) {
                loadStatus.setText(mContent.getResources().getString(R.string.no_loader));
            }
        }

        public void setViewListener(final LineUp lineUp) {
            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.item_view:
                            if (lineUp.getStatus() == 0) {
                                AlertUtils alertUtils = new AlertUtils(mContent);
                                alertUtils.showError(mContent.getResources().getString(R.string.alert_info), "该车辆还没有进入第一次过磅");
                                return;
                            }
                            Intent intent;
                            //已经装车
                            if (lineUp.getIsloadcar() == 2) {
                                intent = new Intent(mContent, LoadCarDetailActivity.class);
                            } else {
                                intent = new Intent(mContent, LoadCarOptionActivity.class);
                            }
                            Bundle bundle = new Bundle();
                            bundle.putLong("lineUpId", lineUp.getId());
                            bundle.putSerializable("lineUp", lineUp);
                            intent.putExtra("lp", bundle);
                            mContent.startActivity(intent);
                            break;
                    }
                }
            });
        }
    }

}
